package com.kangfawei.shiro.service.impl;

import com.kangfawei.shiro.dao.LoginDao;
import com.kangfawei.shiro.dao.UserDao;
import com.kangfawei.shiro.pojo.User;
import com.kangfawei.shiro.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private LoginDao loginDao;

    @Override
    public User getUser(String username) {
        return loginDao.login(username);
    }

    @Override
    public Integer login(String username, String password) throws AuthenticationException{
        Subject curSubject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try {
            curSubject.login(token);
            return 1;
        }catch (UnknownAccountException e){
            return 2;
        }catch (IncorrectCredentialsException e){
            return 3;
        }
        catch (AuthenticationException e) {
            return 4;
        }

    }


}
